<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Timeline
 * @description : Timeline of user activities
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Timeline extends AUTH_Controller {

  /**
   * Just home page
   * 
   */
  public function index() {

	$top_users = [];
	$get_all_users = $this->user_model->get_online_users(10);
	if (!empty($get_all_users['result'])) {
	  $top_users = $get_all_users['result'];
	}

	// Getting last activity
	$activity_days = array();
	$get_last_activity = $this->settings_model->get_last_activity_date();
	if (!empty($get_last_activity['result'])) {
	  foreach ($get_last_activity['result'] as $activity) {
		if (!empty($activity)) {
		  $activity_days[] = $activity;
		}
	  }
	  sort($activity_days);
	}
	$last_active_day = (end($activity_days)) ? strtotime(end($activity_days)) : strtotime(date('Y-m-d'));

	$timeline_options = array(
		'Today' => strtotime(date('Y-m-d')),
		'Last Month' => strtotime("-1 month", strtotime(date('Y-m-01'))),
		'Last 6 Month' => strtotime("-6 month", strtotime(date('Y-m-01'))),
		'Last Year' => strtotime("-1 year", strtotime(date('Y-01-01'))),
		'Last 2 Year' => strtotime("-2 year", strtotime(date('Y-01-01'))),
	);

	$view_data = array();
	$view_data['menu_active'] = 'timeline';
	$view_data['timeline_options'] = $timeline_options;
	$view_data['top_users'] = $top_users;
	$view_data['last_active_day'] = $last_active_day;
	$this->load_view('timeline', $view_data);
	return;
  }

  /**
   * Load timeline content via ajax
   * 
   * @return type
   */
  public function ajax() {

	if ($this->is_post()) {

	  $offset = (int) $this->input->post('offset');
	  $filter = (string) $this->input->post('filter');

	  if (!empty($offset) && is_numeric($offset)) {
		$timelineDate = date('Y-m-d', $offset);
	  } else {
		$timelineDate = date('Y-m-d');
	  }

	  // Getting last activity
	  $activity_days = array();
	  $get_last_activity = $this->settings_model->get_last_activity_date($timelineDate);
	  if (!empty($get_last_activity['result'])) {
		foreach ($get_last_activity['result'] as $activity) {
		  if (!empty($activity)) {
			$activity_days[] = $activity;
		  }
		}
		sort($activity_days);
	  }
	  $last_active_day = (end($activity_days)) ? strtotime(end($activity_days)) : null;

	  // Sorting & Limiting activity data (Last 25)
	  $activity_data = $this->get_activities($filter, $timelineDate, NULL, NULL);
	  @usort($activity_data, array('timeline', 'date_compare'));
	  $activity_data = $this->group_assoc($activity_data, 'grouping_id');
	  //$activity_data = @array_slice($activity_data, 0, 10);
	  // Preparing and Loading view
	  $view_data = array();
	  $view_data['menu_active'] = 'timeline';
	  $view_data['activity_data'] = $activity_data;
	  $view_data['offset'] = $offset;
	  $view_data['timelineDate'] = $timelineDate;
	  $view_data['last_active_day'] = $last_active_day;
	  $this->load->view('timeline_data', $view_data);
	  return;
	}
  }

  private function get_activities($filter = NULL, $timelineDate = NULL, $limit = 10, $offset = 0) {

	// Getting activities
	if (empty($filter) || $filter == 'tweet') {
	  $new_tweets = $this->timeline_model->get_new_tweets($timelineDate, $limit, $offset);
	  $new_replies = $this->timeline_model->get_new_replies($timelineDate, $limit, $offset);
	}
	if (empty($filter) || $filter == 'event') {
	  $new_events = $this->timeline_model->get_new_events($timelineDate, $limit, $offset);
	}
	if (empty($filter) || $filter == 'album') {
	  $new_albums = $this->timeline_model->get_new_albums($timelineDate, $limit, $offset);
	}
	if (empty($filter) || $filter == 'wall_post') {
	  $new_wall_posts = $this->timeline_model->get_new_wall_posts($timelineDate, $limit, $offset);
	}
	if (empty($filter) || $filter == 'poll') {
	  $new_polls = $this->timeline_model->get_new_polls($timelineDate, $limit, $offset);
	  $new_poll_votes = $this->timeline_model->get_new_poll_votes($timelineDate, $limit, $offset);
	}
	if (empty($filter)) {
	  $new_likes = $this->timeline_model->get_new_likes($timelineDate, $limit, $offset);
	  $new_comments = $this->timeline_model->get_new_comments($timelineDate, $limit, $offset);
	}

	// Preparing activity data
	$activity_data = array();

	if (!empty($new_tweets['result']) && is_array($new_tweets['result'])) {
	  foreach ($new_tweets['result'] as $tweet) {
		$activity_data[] = array('grouping_id' => 'tweet_' . $tweet['tweet_id'], 'user_id' => $tweet['user_id'], 'user_name' => $tweet['user_name'], 'user_image' => $tweet['user_image'], 'activity_type' => 1, 'activity_id' => $tweet['tweet_id'], 'activity_date' => $tweet['tweet_date'], 'activity_message' => $tweet['tweet_subject']);
	  }
	}

	if (!empty($new_replies['result']) && is_array($new_replies['result'])) {
	  foreach ($new_replies['result'] as $reply) {
		$activity_data[] = array('grouping_id' => 'tweet_' . $reply['tweet_reply_tweet_id'], 'user_id' => $reply['user_id'], 'user_name' => $reply['user_name'], 'user_image' => $reply['user_image'], 'activity_type' => 2, 'activity_id' => $reply['tweet_reply_id'], 'activity_date' => $reply['tweet_reply_date'], 'activity_message' => $reply['tweet_reply_message']);
	  }
	}

	if (!empty($new_events['result']) && is_array($new_events['result'])) {
	  foreach ($new_events['result'] as $event) {
		$activity_data[] = array('grouping_id' => 'event_' . $event['event_id'], 'user_id' => $event['user_id'], 'user_name' => $event['user_name'], 'user_image' => $event['user_image'], 'activity_type' => 3, 'activity_id' => $event['event_id'], 'activity_date' => $event['event_date'], 'activity_message' => $event['event_message']);
	  }
	}

	if (!empty($new_albums['result']) && is_array($new_albums['result'])) {
	  foreach ($new_albums['result'] as $album) {
		if ($album['album_status'] == 1) {
		  $activity_data[] = array('grouping_id' => 'album_' . $album['album_id'], 'user_id' => $album['user_id'], 'user_name' => $album['user_name'], 'user_image' => $album['user_image'], 'activity_type' => 4, 'activity_id' => $album['album_id'], 'activity_date' => $album['album_date'], 'activity_message' => $album['album_name']);
		}
	  }
	}

	if (!empty($new_likes['result']) && is_array($new_likes['result'])) {
	  foreach ($new_likes['result'] as $like) {
		$activity_data[] = array('grouping_id' => $like['like_type'] . '_' . $like['like_type_id'], 'user_id' => $like['user_id'], 'user_name' => $like['user_name'], 'user_image' => $like['user_image'], 'activity_type' => 5, 'activity_id' => $like['like_type_id'], 'activity_date' => $like['like_date'], 'activity_message' => $this->get_like_data($like['like_type'], $like['like_type_id']), 'activity_item_type' => $like['like_type'], 'activity_image_url' => $this->get_like_data($like['like_type'], $like['like_type_id']));
	  }
	}

	if (!empty($new_wall_posts['result']) && is_array($new_wall_posts['result'])) {
	  foreach ($new_wall_posts['result'] as $wall_post) {
		$activity_data[] = array('grouping_id' => 'wall_post_' . $wall_post['wall_post_id'], 'user_id' => $wall_post['user_id'], 'user_name' => $wall_post['user_name'], 'user_image' => $wall_post['user_image'], 'activity_type' => 6, 'activity_id' => $wall_post['wall_post_id'], 'activity_date' => $wall_post['wall_post_date'], 'activity_message' => $wall_post['wall_post_message']);
	  }
	}

	if (!empty($new_polls['result']) && is_array($new_polls['result'])) {
	  foreach ($new_polls['result'] as $user_poll) {
		$activity_data[] = array('grouping_id' => 'poll_' . $user_poll['poll_id'], 'user_id' => $user_poll['user_id'], 'user_name' => $user_poll['user_name'], 'user_image' => $user_poll['user_image'], 'activity_type' => 7, 'activity_id' => $user_poll['poll_id'], 'activity_date' => $user_poll['poll_date'], 'activity_message' => $user_poll['poll_message']);
	  }
	}

	if (!empty($new_poll_votes['result']) && is_array($new_poll_votes['result'])) {
	  foreach ($new_poll_votes['result'] as $user_poll_vote) {
		$activity_data[] = array('grouping_id' => 'poll_' . $user_poll_vote['poll_id'], 'user_id' => $user_poll_vote['user_id'], 'user_name' => $user_poll_vote['user_name'], 'user_image' => $user_poll_vote['user_image'], 'activity_type' => 8, 'activity_id' => $user_poll_vote['poll_id'], 'activity_date' => $user_poll_vote['poll_reply_date'], 'activity_message' => $user_poll_vote['poll_message']);
	  }
	}

	if (!empty($new_comments['result']) && is_array($new_comments['result'])) {
	  foreach ($new_comments['result'] as $user_comment) {
		$activity_data[] = array('grouping_id' => $user_comment['comment_type'] . '_' . $user_comment['comment_type_id'], 'user_id' => $user_comment['user_id'], 'user_name' => $user_comment['user_name'], 'user_image' => $user_comment['user_image'], 'activity_type' => 9, 'activity_id' => $user_comment['comment_type_id'], 'activity_date' => $user_comment['comment_date'], 'activity_message' => $user_comment['comment_message'], 'activity_item_type' => $user_comment['comment_type'], 'activity_image_url' => $this->get_like_data($user_comment['comment_type'], $user_comment['comment_type_id']));
	  }
	}

	return $activity_data;
  }

  /**
   * Get data of the item user liked
   */
  private function get_like_data($like_type = null, $like_type_id = null) {

	$data_text = null;

	// Getting the post
	if ($like_type == 'tweet') {
	  $like_type_data = $this->tweet_model->get_tweet_by_id($like_type_id);
	  if (!empty($like_type_data['result'])) {
		$data_text = $like_type_data['result']['tweet_subject'];
	  }
	} else if ($like_type == 'reply') {
	  $like_type_data = $this->tweet_model->get_reply_by_id($like_type_id);
	  if (!empty($like_type_data['result'])) {
		$data_text = $like_type_data['result']['tweet_reply_message'];
	  }
	} else if ($like_type == 'event') {
	  $like_type_data = $this->event_model->get_event_by_id($like_type_id);
	  if (!empty($like_type_data['result'])) {
		$data_text = $like_type_data['result']['event_message'];
	  }
	} else if ($like_type == 'album') {
	  $like_type_data = $this->album_model->get_album_by_id($like_type_id);
	  if (!empty($like_type_data['result'])) {
		$data_text = $like_type_data['result']['album_name'];
	  }
	} else if ($like_type == 'photo') {
	  $like_type_data = $this->album_model->get_photo_by_id($like_type_id);
	  if (!empty($like_type_data['result'])) {
		$data_text = base_url('assets/albums/' . $like_type_data['result']['photo_album_id'] . '/' . substr($like_type_data['result']['photo_file'], 0, 32) . '_thumb.jpg');
	  }
	} else if ($like_type == 'video') {
	  $like_type_data = $this->album_model->get_photo_by_id($like_type_id);
	  if (!empty($like_type_data['result'])) {
		$data_text = base_url('assets/albums/' . $like_type_data['result']['photo_album_id'] . '/' . substr($like_type_data['result']['photo_file'], 0, 32) . '_thumb.jpg');
	  }
	} else if ($like_type == 'wall_post') {
	  $like_type_data = $this->wall_model->get_wall_post_by_id($like_type_id);
	  if (!empty($like_type_data['result'])) {
		$data_text = $like_type_data['result']['wall_post_message'];
	  }
	} else if ($like_type == 'poll') {
	  $like_type_data = $this->poll_model->get_poll_by_id($like_type_id, NULL);
	  if (!empty($like_type_data['result'])) {
		$data_text = $like_type_data['result']['poll_message'];
	  }
	} else if ($like_type == 'comment') {
	  $like_type_data = $this->comment_model->get_comment_by_id($like_type_id);
	  if (!empty($like_type_data['result'])) {
		$data_text = $like_type_data['result']['comment_message'];
	  }
	}

	return $data_text;
  }

  /**
   * Fuction to sort according to date
   * 
   */
  public function date_compare($a, $b) {
	$t1 = strtotime(date('Y-m-d H:i:s', strtotime($a['activity_date'])));
	$t2 = strtotime(date('Y-m-d H:i:s', strtotime($b['activity_date'])));
	return $t2 - $t1;
  }

  /**
   * Function to group array
   * 
   */
  function group_assoc($array, $key) {
	$return = array();
	foreach ($array as $v) {
	  if (empty($return[$v[$key]])) {
		$return[$v[$key]] = $v;
	  }
	}
	return $return;
  }

}

?>